﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Win32;
using System.Windows;
using System.Xml;

namespace SHomeWorkshop.LunarMind.Commands
{
    /// <summary>
    /// 以此类为模板，制作自定义命令类
    ///     共需要修改１０处！！！
    /// ==============================
    /// 
    /// 全选命令。
    /// ★①，修改类名。
    /// </summary>
    public static class SelectAllCommand
    {
        private static RoutedUICommand routedUICmd;//创建私有字段

        public static RoutedUICommand RoutedUICmd//创建只读静态属性
        {
            get { return routedUICmd; }
        }

        /// <summary>
        /// ★②，修改静态构造方法名。
        /// </summary>
        static SelectAllCommand()//类型构造器
        {
            //★③，修改两个字符串参数名。★④以及第三个参数的类型名。
            routedUICmd = new RoutedUICommand(
                "SelectAllCommand",
                "SelectAllCommand",
                typeof(SelectAllCommand),//创建RoutedUICommand对象
                new InputGestureCollection() 
                { 
                    //★⑤，修改此处两个参数，以便挂接快捷键。
                    new KeyGesture(Key.A,ModifierKeys.Control,"Ctrl+A")
                });

            cmdBinding.Command = routedUICmd;
            cmdBinding.CanExecute += new CanExecuteRoutedEventHandler(cmdBinding_CanExecute);
            cmdBinding.Executed += new ExecutedRoutedEventHandler(cmdBinding_Executed);
        }

        private static CommandBinding cmdBinding = new CommandBinding();

        public static CommandBinding CmdBinding
        {
            get { return cmdBinding; }
        }

        static void cmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //★⑦，修改此方法的实现。

            SelectAll();
        }

        /// <summary>
        /// 此方法是调用DeleteCommand的Delete来实现。
        /// </summary>
        internal static void SelectAll()
        {
            if (Globals.MainWindow.SelectedRelation != null)
                Globals.MainWindow.SelectedRelation = null;

            if (Globals.MainWindow.SelectedTitleImage != null)
                Globals.MainWindow.SelectedTitleImage = null;

            Globals.MainWindow.SelectedTitlesList.Clear();

            if (Globals.MainWindow.mainCanvas.Children.Count > 0)
            {
                foreach (UIElement ue in Globals.MainWindow.mainCanvas.Children)
                {
                    Title t = ue as Title;
                    if (t != null)
                    {
                        Globals.MainWindow.SelectedTitlesList.Add(t);
                        t.IsSelected = true;
                        t.IsMainSelectedTitle = false;
                    }
                }

                Globals.MainWindow.RootTitle.IsMainSelectedTitle = true;
            }
        }

        static void cmdBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;//全选，总是有效的。

        }
    }
}
